<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\shop\controller;

use app\shop\model\OrderModel;
use cmf\controller\HomeBaseController;
use think\Db;

class OrderController extends HomeBaseController
{
    function index(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
           
           $order=new OrderModel();
           $order_info=$order->get_order();
            if($order_info==1){
                $this->redirect('/shop/good/car.html');
            }
            $this->assign('message',$order_info['message']);
            $this->assign('count_yifu_price',$order_info['count_yifu_price']);
            $this->assign('count_yifu_score',$order_info['count_yifu_score']);
            $this->assign('data',$order_info);
            $this->assign('address_list',$order_info['address_list']);
            $this->assign('car_list',$order_info['car_list']);
            $this->assign('fright',$order_info['fright']);
            return $this->fetch(":order");
        }else{
            $this->redirect('user/login/index');
        }
    }
    function index_card(){

        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $param=$this->request->param();
            $id=$param['id'];
            $info=Db::name('card')->alias('a')->join('store','store.id=a.store_id')->where('a.card_id',$id)->find();
            $gundong=0;

            if($info['type']==4){
                $time=time();
                $where['card_end_time']=['>',$time];
                $where['card_type']=4;
                $gundong=Db::name('user')->where($where)->count();
            }
            $uid=cmf_get_current_user_id();
            $user_info=Db::name('user')->where('id',$uid)->find();
            $card_type=0;
            if($user_info['card_type']&&$user_info['card_type']!=5){
                $time=time();
                if($user_info['card_end_time']>$time){
                    $card_type=1;
                }
            }
            if($user_info['card_type']&&$user_info['card_type']==5){
                $card_type=1;
            }

            $this->assign('card_type',$card_type);
            $this->assign('info',$info);
            $this->assign('gundong',$gundong);
            return $this->fetch(":order_card");
        }else{
            $this->redirect('user/login/index');
        }
    }

    /**
     *  $order['order_no'];//订单编号
     *  $order['buyer_id'];//买家id
     *  $order['user_name'];//买家会员名称
     *  $order['buyer_ip'];//买家ip
     *  $order['buyer_message'];//买家附言
     *  $order['is_invoice'];//是否开发票  1为开发票
     *  $order['invoice_type'];//发票类型 0 个人 1公司
     *  $order['invoice_name'];//发票人公司名称
     *  $order['invoice_code'];//发票人识别号
     *  $order['receiver_mobile'];//收货人的手机号码
     *  $order['receiver_province'];//收货人所在省
     *  $order['receiver_city'];//收货人所在城市
     *  $order['receiver_district'];//收货人所在街道
     *  $order['receiver_address'];//收货人详细地址
     *  $order['receiver_name'];//收货人姓名
     *  $order['shop_id'];//关联店铺id
     *  $order['goods_money'];//订单现金总价
     *  $order['goods_point'];//订单总积分
     *   $order['shipping_money'];//订单运费
     *   $order['pay_point'];//订单实际消耗积分
     *   $order['pay_money'];//订单实际支付金额
     *  $order['order_status']=0;//订单状态
     *   $order['is_evaluate']=0;//是否评价 0为未评价 1为已评价 2为已追评
     *   $order['create_time']=time();//订单创建时间
     *   $order['leavemessage'];//留言
     *
     * $good['order_id']//订单ID
     * $good['goods_id']//商品ID
     * $good['goods_name']//商品名称
     * $good['price']//商品价格
     * $good['point']//积分价
     * $good['num']//购买数量
     * $good['goods_money']//商品总价
     * $good['goods_point']//商品总积分价
     * $good['goods_picture']//商品图片
     * $good['shop_id']//关联店铺id
     * $good['buyer_id']//买家id
     */
    function dopay(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $param=request()->param();
            $order_model=new OrderModel();
            $order_info=$order_model->get_order();
            $user=cmf_get_current_user();
            //收货地址
            $address_list=$order_info['address_list'][0];
            //oreder_数据准备
            $param['leavemessage']=isset($param['leavemessage'])?$param['leavemessage']:'';
            $param['h_receipt']=isset($param['h_receipt'])?$param['h_receipt']:'';
            $param['h_receipt_type']=isset($param['h_receipt_type'])?$param['h_receipt_type']:'';
            $param['receipt_info_name']=isset($param['receipt_info_name'])?$param['receipt_info_name']:'';
            $param['receipt_info_code']=isset($param['receipt_info_code'])?$param['receipt_info_code']:'';
            $order=[
                'order_no'=>cmf_get_order_sn(),
                'buyer_id'=>$user['id'],
                'user_name'=>$user['mobile'],
                'buyer_ip'=>get_client_ip(0, true),
                'buyer_message'=>$param['leavemessage'],
                'is_invoice'=>$param['h_receipt'],
                'invoice_type'=>$param['h_receipt_type'],
                'invoice_name'=>$param['receipt_info_name'],
                'invoice_code'=>$param['receipt_info_code'],
                'receiver_mobile'=>$address_list['mobile'],
                'receiver_province'=>$address_list['province'],
                'receiver_city'=>$address_list['city'],
                'receiver_district'=>$address_list['district'],
                'receiver_address'=>$address_list['address'],
                'receiver_name'=>$address_list['consigner'],
                'shop_id'=>$user['store_id'],
                'goods_money'=>$order_info['count_post_price'],
                'goods_point'=>$order_info['count_post_score'],
                'shipping_money'=>$order_info['fright'],
                'pay_money'=>$order_info['count_yifu_price'],
                'pay_point'=>$order_info['count_yifu_score'],
                'create_time'=>time(),
                'leavemessage'=>$param['leavemessage']
            ];

            $car_list=$order_info['car_list'];
            $flag=false;
            Db::startTrans();
            try{
               $order_rs=Db::name('order')->insert($order,false,true);
                foreach($car_list as $k=>$v){
                    $good=[
                        'order_id'=>$order_rs,
                        'goods_id'=>$v['good_id'],
                        'goods_name'=>$v['goods']['post_title'],
                        'price'=>$v['goods']['post_price'],
                        'point'=>$v['goods']['post_score'],
                        'num'=>$v['num'],
                        'goods_money'=>$v['goods']['post_price']*$v['num'],
                        'goods_point'=>$v['goods']['post_score']*$v['num'],
                        'goods_picture'=>$v['goods']['more'],
                        'shop_id'=>$user['store_id'],
                        'buyer_id'=>$user['id'],
                    ];
                    $good_rs=Db::name('order_goods')->insert($good);
                    Db::name('car')->delete($v['id']);

                }
                // 提交事务
                Db::commit();
                $flag=true;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if($flag){
                $order_no=$order['order_no'];
             	 session('ordr_type',0);
                $url=cmf_url('shop/order/pay',array('order_no'=>$order_no));
                $this->success("下单成功",$url);
            }else{
                $this->success("下单失败");
            }

        }else{
           $this->error('请先登录！');
        }
    }


    function dopay_card(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $param=request()->param();
            $card_id=$param['card_id'];
            $info=Db::name('card')->alias('a')->join('store','store.id=a.store_id')->where('a.card_id',$card_id)->find();
        
            if($info['type']==4){
                $time=time();
                $where['card_end_time']=['>',$time];
                $where['card_type']=4;
                $gundong=Db::name('user')->where($where)->count();
                if($gundong>=10){
                    $this->error("股东家庭年卡已售光!");
                }
            }


            $user=cmf_get_current_user();
            $start_time=date('Y-m-d',time());
            if($info['type']==1){
                $end_time=date('Y-m-d  23:59:59',strtotime("+1 week"));
            }
            if($info['type']==2){
                $end_time=date('Y-m-d  23:59:59',strtotime("+6 month"));
            }
            if($info['type']==3){
                $end_time=date('Y-m-d  23:59:59',strtotime("+12 month"));
            }
            if($info['type']==4){
                $end_time=date('Y-m-d  23:59:59',strtotime("+12 month"));
            }
            $order=[
                'order_no'=>cmf_get_order_sn(),
                'card_id'=>$info['card_id'],
                'store_id'=>$info['store_id'],
                'buyer_ip'=>get_client_ip(0, true),
                'uid'=>$user['id'],
                'price'=>$info['price'],
                'start_time'=>strtotime($start_time),
                'end_time'=>strtotime($end_time),
                'is_jiesuan'=>0,                
                'type'=>$info['type'],
            ];
            $flag=false;
            Db::startTrans();
            try{
                $order_rs=Db::name('order_card')->insert($order,false,true);
                Db::commit();
                $flag=true;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if($flag){
                $order_no=$order['order_no'];
              	session('ordr_type',1);
                $url=cmf_url('shop/order/pay',array('order_no'=>$order_no));
                $this->success("下单成功,正在跳转支付中...",$url);
            }else{
                $this->success("下单失败");
            }
        }else{
            $this->error('请先登录！');
        }
    }


    function pay(){
        $type=session('ordr_type');
        if($type==0){
            $order_no=request()->param('order_no');
            $order_no=isset($order_no)?$order_no:null;
            if($order_no){
                $order_no=request()->param('order_no');
                $order_no=isset($order_no)?$order_no:null;
                if($order_no){
                    $order_no=str_replace('.htmls=','',$order_no);
                    $where['order_no']=$order_no;
                    $order_info=Db::name('order')->where($where)->find();
                    //生成临时订单号
                    Db::name('order_weixin')->where($where)->delete();
                    $where['order_no_weixin']=$order_no.time();
                    $weixin_order=Db::name('order_weixin')->insert($where,false,true);
                    $weixin_order_info=Db::name('order_weixin')->where('id',$weixin_order)->find();
                    $info['Body']="订单：".$weixin_order_info['order_no_weixin'];
                    $info['Attach']="7S";
                    $info['Out_trade_no']=$weixin_order_info['order_no_weixin'];
                    $info['Total_fee']=$order_info['pay_money'];
                 $info['order_status']=$order_info['order_status'];
                    hook("wechatpay", $info);
                }
            }
        }else{
            $order_no=request()->param('order_no');
            $order_no=isset($order_no)?$order_no:null;
            if($order_no){
                $order_no=request()->param('order_no');
                $order_no=isset($order_no)?$order_no:null;
                if($order_no){
                    $order_no=str_replace('.htmls=','',$order_no);
                    $where['order_no']=$order_no;
                    $order_info=Db::name('order_card')->where($where)->find();
                    //生成临时订单号
                    Db::name('order_weixin')->where($where)->delete();
                    $where['order_no_weixin']=$order_no.time();
                    $where['type']=1;
                    $weixin_order=Db::name('order_weixin')->insert($where,false,true);
                    $weixin_order_info=Db::name('order_weixin')->where('id',$weixin_order)->find();
                    $info['Body']="订单：".$weixin_order_info['order_no_weixin'];
                    $info['Attach']="7S";
                    $info['Out_trade_no']=$weixin_order_info['order_no_weixin'];
                    $info['Total_fee']=$order_info['price']; 
                    $info['order_status']=$order_info['pay_status'];
                    hook("wechatpay", $info);
                }
            }
        }


    }

  
   function notify(){
        $xml = file_get_contents('php://input');
      	hook("wechatnotify", $xml);
    }
}
